package com.example.administrator.baikangxing.activity;

import android.content.Intent;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.AlphaAnimation;
import android.view.animation.AnimationSet;
import android.widget.ImageView;

import com.example.administrator.baikangxing.Dao.OperateData;
import com.example.administrator.baikangxing.R;

public class SplashActivity extends FragmentActivity {

    private Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message message) {
            return false;
        }
    });
    private ImageView splash_iv;
    private int userstate;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_splash_activity);
        initView();
        initData();
        //透明状态栏  
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            Window window = getWindow();
            // Translucent status bar  
            window.setFlags(
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        //透明状态栏
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        /*//透明导航栏  
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);*/
    }

    private void initData() {
        AnimationSet animationSet = new AnimationSet(true);
        AlphaAnimation alphaAnimation = new AlphaAnimation(0, 1);
        alphaAnimation.setDuration(3000);
        animationSet.addAnimation(alphaAnimation);
        splash_iv.startAnimation(animationSet);
        userstate = OperateData.getIntData(SplashActivity.this, "userstate");
        if (userstate == 0) {
            // 执行了一个延时任务,跳转主页面
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    enterLoging();
                }
            }, 2000);
        } else if (userstate == 1) {
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    enterMain();
                }
            }, 2000);
        }
    }

    private void initView() {
        splash_iv = (ImageView) findViewById(R.id.splash_iv);
    }


    /**
     * 进入应用的登录页面
     */
    private void enterLoging() {
        //判断应用是否第一次被打开
        int guide_state = OperateData.getIntData(SplashActivity.this, "guide_state");
        if (guide_state == 0) {//进入引导界面
            startActivity(new Intent(SplashActivity.this,NewSplashActivity.class));
        } else if (guide_state == 1) {//进入登录页面
            startActivity(new Intent(this,LoginActivity.class));
        }
        finish();
    }

    /**
     * 进入应用的主页面
     */
    private void enterMain() {
        Intent intent = new Intent(SplashActivity.this, MainActivity.class);
        startActivity(intent);
        finish();
    }

}
